#!/usr/bin/env python
# encoding: utf-8
from RegexCaller import RegexCaller
from BeautifulSoup import BeautifulSoup
from Debug import *

class HorizontalParser:
	def __init__(self,Content):
		self.Regex = RegexCaller()
		self.Regex.Add("Tags","<.*?>.*</.*?>")
		self.Regex.Add("Conditionals","<!\[.*?>")
		self.Regex.Add("Comment","<!--.*?>")
		self.Regex.Add("Tag","<.*?>")
		self.Regex.Add("Spaces"," ")
		self.Regex.Add("Headers","<th")
		self.Regex.Add("Escapes","&....;")
		self.Regex.Add("Tabs","[\t]")
		self.Regex.Add("Newlines","[\n]")
		self.Regex.Add("\R","\r")
		self.Regex.Add("Quotes","\"")
		self.Regex.Add("Clean","<table.*?>.*?</table.*>")
		self.Content = ''.join(self.Regex.FindAll(Content,"Clean"))
		DebugPrint(Content)
		self.Content = self.Regex.Replace(self.Content,"Conditionals","")
		self.Content = self.Regex.Replace(self.Content,"Comment","")
		self.Content = self.Regex.Replace(self.Content,"Headers","<td")
		self.Content = self.Regex.Replace(self.Content,"Escapes","")
		self.Content = self.Regex.Replace(self.Content,"Tabs"," ")
		self.Content = self.Regex.Replace(self.Content,"\R","")
		self.Content = self.Regex.Replace(self.Content,"Quotes","'")
		self.Soup = BeautifulSoup(self.Content)
		self.Tables = self.Soup.findAll("table")
	
	def GetTableName(self,TableIndex):
		return self.Regex.Replace(str(self.Tables[TableIndex].tr.td),"Tag","").strip()
	
	def GetTableHeaders(self,TableIndex,HasTitle = False):
		Table = self.Tables[TableIndex]
		i = 0
		if HasTitle:
			i = 1
		Row = Table.findAll("tr")[i]
		Headers = []
		for Col in Row.findAll("td"):
			Headers.append(self.Regex.Replace(self.Regex.Replace(str(Col),"Tag",""),"Spaces","_").strip())
		return Headers	
	
	def GetTableData(self,TableIndex,HasTitle=False,HasHeaders=False,NewHeaders=None,IndexBy=None,SkipRows=0,ProcessRows=None):
		Table = self.Tables[TableIndex]
		Data = {}
		i = SkipRows
		if HasTitle:
			i = i + 1
		if HasHeaders:
			i = i + 1
			if NewHeaders == None:
				NewHeaders = self.GetTableHeaders(TableIndex,HasTitle)
		if ProcessRows != None:
			ProcessRows = ProcessRows+i
		DataRows = Table.findAll("tr")[i:ProcessRows]
		i = 0
		for Row in DataRows:
			i = i + 1
			RowData = {}
			RowCol = Row.td
			for Col in NewHeaders:
				RowData[str(Col)] = self.Regex.Replace(str(RowCol),"Tag","").strip()
				RowCol = RowCol.findNextSibling()	
			if IndexBy == None:
				Index = i
			else:
				Index = RowData[IndexBy]
			Data[Index] = RowData
		return Data	
	


class VerticalParser:
	def __init__(self,Content):
		self.Regex = RegexCaller()
		self.Regex.Add("Tags","<.*?>.*</.*?>")
		self.Regex.Add("Conditionals","<!\[.*?>")
		self.Regex.Add("Comment","<!--.*?>")
		self.Regex.Add("Tag","<.*?>")
		self.Regex.Add("Spaces"," ")
		self.Regex.Add("Headers","<th")
		self.Regex.Add("Escapes","&....;")
		self.Regex.Add("Tabs","[\t]")
		self.Regex.Add("Newlines","[\n,\r]")
		self.Regex.Add("Quotes","\"")
		self.Regex.Add("Clean","<t.*?>.*</t.*>")
		self.Content = ''.join(self.Regex.FindAll(Content,"Clean"))
		self.Content = self.Regex.Replace(self.Content,"Tabs","")
		self.Content = self.Regex.Replace(self.Content,"Conditionals","")
		self.Content = self.Regex.Replace(self.Content,"Comment","")
		self.Content = self.Regex.Replace(self.Content,"Headers","<td")
		self.Content = self.Regex.Replace(self.Content,"Escapes","")
		self.Content = self.Regex.Replace(self.Content,"Tabs"," ")
		self.Content = self.Regex.Replace(self.Content,"Quotes","'")
		self.Soup = BeautifulSoup(self.Content)
		self.Tables = self.Soup.findAll("table")
	
	def GetTableName(self,TableIndex):
		return self.Regex.Replace(str(self.Tables[TableIndex].tr.td),"Tag","").strip()
	
	def GetTableHeaders(self,TableIndex,HasTitle = False):
		Table = self.Tables[TableIndex]
		i = 0
		if HasTitle:
			i = 1
		Headers = []
		Rows = Table.findAll('tr')[i:]
		for el in Rows:
			Headers.append(self.Regex.Replace(self.Regex.Replace(str(el.td),"Tag",""),"Spaces","_").strip())
		return Headers
	
	def GetTableData(self,TableIndex,HasTitle=False,HasHeaders=False,NewHeaders=None,IndexBy=None,SkipRows=0,ProcessRows=None):
		Table = self.Tables[TableIndex]
		DataDict = {}
		Data = []
		i = SkipRows
		if HasTitle:
			i = i + 1
		if HasHeaders and NewHeaders != None:
			NewHeaders = self.GetTableHeaders(TableIndex,HasTitle)
		if ProcessRows != None:
			ProcessRows = ProcessRows+i
		Rows = Table.findAll("tr")[i:ProcessRows]
		i = 0
		for Row in Rows:
			i = i + 1
			RowData = []
			Cols = Row.findAll("td")[1:]
			for Col in Cols:
				RowData.append(str(Col).strip())
			Data.append(RowData)
		if len(Data) == 0:
			return {}
		for i in range(0,len(Data[0])):
			RowDict = {}
			for j in range(0,len(Data)):
				RowDict[NewHeaders[j]]=self.Regex.Replace(str(Data[j][i]),"Tag","").strip()
			if IndexBy != None:
				DataDict[RowDict[IndexBy]] = RowDict
			else:
				DataDict[i] = RowDict
		return DataDict
	